package cn.tedu.egcar1.controller;

import cn.tedu.egcar1.pojo.dto.VehicleListQuery;
import cn.tedu.egcar1.base.response.JsonResult;
import cn.tedu.egcar1.base.response.StatusCode;
import cn.tedu.egcar1.mapper.VehicleMapper;
import cn.tedu.egcar1.pojo.dto.VehicleAddParam;
import cn.tedu.egcar1.pojo.entity.Vehicle;
import cn.tedu.egcar1.pojo.vo.UserRegQuery;
import cn.tedu.egcar1.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/vehicle/")
@Slf4j
@Api(tags = "02.车辆管理")
public class VehicleController {
    @Autowired
    VehicleMapper vehicleMapper;
    @ApiOperation("添加车辆")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session){
        log.debug("vehicleAddParam="+vehicleAddParam);
        //判断用户是否登录了
        UserRegQuery userRegQuery=(UserRegQuery)session.getAttribute("user");
        if (userRegQuery==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        //已登录的话就行添加操作
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userRegQuery.getId());
        vehicleMapper.add(vehicle);
        log.debug("vehicle="+vehicle);
        return JsonResult.ok(vehicle);
    }
    @GetMapping("list")
    @ApiOperation("车辆列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(VehicleListQuery vehicleListQuery, @ApiIgnore HttpSession session){
        log.debug("vehicleListQuery="+vehicleListQuery);
        //还是要先判断是否登录了
        UserRegQuery userRegQuery=(UserRegQuery) session.getAttribute("user");
        if (userRegQuery==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        //如果登录了
        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        Long userId = userRegQuery.getId();
        List<VehicleListVO> vehicleListVOS=vehicleMapper.selectByVehicle(brand,license,userId);
        log.debug("vehicleListVOS:"+vehicleListVOS);
        return JsonResult.ok(vehicleListVOS);
    }
    @PostMapping("del/{id}")
    @ApiOperation("删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteVehicle(@PathVariable Integer id, @ApiIgnore HttpSession session){
        log.debug("id:"+id);
        //还是要先判断一下是否已经登录了
        UserRegQuery userRegQuery = (UserRegQuery) session.getAttribute("user");
        if (userRegQuery==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        //如果是登录状态,就调用mapper的delete方法
        vehicleMapper.deleteVehicleById(id,userRegQuery.getId());
        return JsonResult.ok();
    }
}
